package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.BangDanAdatper;
import com.xgkj.diyiketang.data.AnchorInfo;
import com.xgkj.diyiketang.data.GameInfo;
import com.xgkj.diyiketang.data.SellerInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.BangDanTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/18.
 */
public class BangDanActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener{
    private Context mContext;
    private PullToRefreshListView mListView;
    private BangDanAdatper mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private String gameid;
    private int mSellerTask = -1;
    private TextView back ;
    private TextView mGameName;
    private TextView mDesc;
    private TextView mBmrs;
    private String mState;
    private Button mBaoMingBtn;
    private String fname;
    private  String gmid;
    private String xieyiUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bangdanlist);
        mContext = this;
        initView();
    }

    private void initView(){
        Intent intent = getIntent();
        gameid = intent.getStringExtra("GAMEID");
       // PalLog.e("IDIDIDID",gameid);
        mState = intent.getStringExtra("STATE");
        mBaoMingBtn = (Button) findViewById(R.id.baoming_btn);
        if(mState.equals("0")){
            mBaoMingBtn.setVisibility(View.VISIBLE);
            mBaoMingBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent bisaiIntent = new Intent(mContext, CanjiahuodongActivity.class);
                    bisaiIntent.putExtra("gameId",gmid);
                    bisaiIntent.putExtra("gameName",fname);
                    bisaiIntent.putExtra("url",xieyiUrl);
                    EatShowUserInfo info = EatShowApp.getInstance().getmUserInfo();
                    if (info != null){
                        mContext.startActivity(bisaiIntent);
                    }else {
                        Intent login_intent = new Intent(mContext,LoginActivity.class);
                        mContext.startActivity(login_intent);
                    }
                }
            });
        }
        mGameName = (TextView) findViewById(R.id.gameName);
        mDesc = (TextView) findViewById(R.id.xyb_desc);
        mBmrs = (TextView) findViewById(R.id.xyb_bmrs);

        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (PullToRefreshListView)findViewById(R.id.bangdanlist);
        mListView.init(mContext,true);
        mAdapter = new BangDanAdatper(mContext);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
        // getSellerList();

    }

    private void getBangDanList(){
        if(mSellerTask != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        BangDanTask stask = new BangDanTask(size,currentPage,gameid);
        stask.addListener(bangdanlistener);
        mSellerTask = getEatShowApp().getTaskManager().addTask(stask);

    }


    private TaskEventListener bangdanlistener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == mSellerTask){
                mSellerTask = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                }else{
                    mListView.onLoadDataComplete(true);
                }
                List<AnchorInfo> mfoodlist = new ArrayList<AnchorInfo>();
                if(data != null){
                    // mfoodlist = (List<SellerInfo>) data;
                    int pageCount = 0;
                    int currentPage = 0;
                    int nextPage = 0;

                    JSONObject jsonString = (JSONObject) data;
                    currentPage = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
                    pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
                    nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
                    fname = jsonString.getString("gameName");
//                    PalLog.e("游戏名称",fname);
                    gmid = jsonString.getString("gameId");
                    xieyiUrl = jsonString.getString("agreeUrl");
                    String desc = jsonString.getString("description");
                    String bmrs = jsonString.getString("signCount");
                    mDesc.setText(desc);
                    mBmrs.setText("已报名人数："+bmrs);
                    mAdapter.setGameid(gmid);
                    mGameName.setText(fname);
//                    String flogo = jsonString.getString("foodBigLogo");
//                    String fnote = jsonString.getString("foodIntroduction");
//                    if(flogo != null && flogo.length()>0){
//                        ImageCache.getInstance(mContext).loadBitmapToView(foodImg, flogo, null);
//                    }
//                    foodName.setText(fname);
//                    foodIntro.setText(fnote);
                    mfoodlist = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), AnchorInfo.class);
                }

                if(mfoodlist != null && mfoodlist.size()>0){
                    mAdapter.updateData(mfoodlist);
                }

                currentPage = result;


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == mSellerTask){
                mSellerTask = -1;
                if(currentPage == 1){
                    mListView.onRefreshComplete();
                }else{
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };


    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getBangDanList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getBangDanList();
    }

    public void onHiddenChanged(boolean hidden) {
        onHiddenChanged(hidden);
        if(!hidden){
            //mListView.invalidate();
            if(mAdapter != null && mAdapter.getCount() == 0){
                mListView.doRefresh();
            }

        }
    }
}
